package shc.graduation.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import shc.graduation.R;
import shc.graduation.utils.SystemBarTintManager;

/**
 * Created by shuhuiChen on  2016/3/26  10:29.
 * Email ：shuhuiCSH@126.com
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Toolbar mToolBar = null;
    protected Toast mToast = null;
    protected Context mContext = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        onPreSetContentView();
        int contentViewId = getContentViewId();
        if (contentViewId <= 0) {
            throw new IllegalArgumentException("the contentview layout id must be setted");
        }

        setContentView(contentViewId);
        View decorView = findViewById(android.R.id.content);
        onPostSetContentView();
        onCreate(savedInstanceState, decorView);
    }

    /**
     * 设置ContentView后
     */
    private void onPostSetContentView() {
        mContext = this;
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
        findViews();
        setSupportActionBar(mToolBar);
    }

    /**
     * 设置contentView前
     */
    private void onPreSetContentView() {
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //状态栏透明，需要在创建SystemBarTintManager之前调用
                setTranslucentStatus(true);
            }
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            //设置状态栏与ToolBar的颜色保持一致
            tintManager.setStatusBarTintResource(R.color.colorPrimaryDark);
        }
    }

    private void setTranslucentStatus(boolean on) {
        Window window = getWindow();
        WindowManager.LayoutParams winParams = window.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        window.setAttributes(winParams);
    }

    public void enabledBackButton() {
        mToolBar.setNavigationIcon(R.mipmap.ic_back);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void toast(String toastString) {
        if (mToast == null) {
            mToast = Toast.makeText(getApplicationContext(), toastString, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(toastString);
        }
        mToast.show();
    }

    /**
     * 跳转Activity，无数据参数
     *
     * @param classObj,isClose
     */
    protected void forward(Class<?> classObj, boolean isFinish) {
        Intent intent = new Intent();
        intent.setClass(this, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        if (isFinish)
            this.finish();
    }

    /**
     * 跳转Activity，有数据参数
     *
     * @param classObj
     * @param params
     * @param isFinish
     */
    protected void forward(Class<?> classObj, Bundle params, boolean isFinish) {
        Intent intent = new Intent();
        intent.setClass(this, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(params);
        this.startActivity(intent);
        if (isFinish)
            this.finish();
    }

    public abstract void onCreate(Bundle saveInsatanceState, View decorView);

    public abstract int getContentViewId();

    public abstract void findViews();
}
